package com.miya.demo.service.impl;

import cn.hutool.core.map.MapUtil;
import com.miya.demo.common.util.OkHttpUtil;
import com.miya.demo.model.vo.dingtalk.AccessTokenVO;
import com.miya.demo.service.DingtalkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * impl dingtalk服务
 *
 * @author CaiXiaowei
 * @date 2022/09/01
 */
@Slf4j
@Service
public class DingtalkServiceImpl implements DingtalkService {

    @Value("${dingtalk.appkey:dingmc7uafsqmcf46tot}")
    private String appkey;

    @Value("${dingtalk.appsecret:ZSPsWxHK2xwMoOfAXb5tpbgqmD5Mf5ECykIt4hnVmMu0DKME22KsM16bGctHTc8Q}")
    private String appsecret;

    /**
     * 获取访问令牌
     *
     * @return {@link AccessTokenVO}
     */
    @Override
    public AccessTokenVO getAccessToken() {
        HashMap<String, Object> paramMap = MapUtil.newHashMap();
        paramMap.put("appkey", appkey);
        paramMap.put("appsecret", appsecret);
        AccessTokenVO accessTokenVO = OkHttpUtil.builder()
                .url("https://oapi.dingtalk.com/gettoken")
                .get(paramMap)
                .sync(AccessTokenVO.class);
        return accessTokenVO;
    }

}
